/**************************************************************************
** Yabet is bloging editor tool that supports many blog's formats. It is very usefull for crossposting, allows easily to
** make posts to your blogs.
**
** Copyright (C) 2010 Valery Konchin <eeexception@gmail.com>
**
** This program is free software: you can redistribute it and/or modify
**     it under the terms of the GNU General Public License as published by
**     the Free Software Foundation, either version 3 of the License, or
**     (at your option) any later version.
**
**     This program is distributed in the hope that it will be useful,
**     but WITHOUT ANY WARRANTY; without even the implied warranty of
**     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**     GNU General Public License for more details.
**
**     You should have received a copy of the GNU General Public License
**     along with this program.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************/

#include "yabetloghandler.h"
#include <QSettings>
#include <QDebug>
#include <QRegExp>
#include <QStringList>

const int YabetLogHandler::LOG_LEVEL_INFO = 4;
const int YabetLogHandler::LOG_LEVEL_DEBUG = 8;
const int YabetLogHandler::LOG_LEVEL_WARNING = 2;
const int YabetLogHandler::LOG_LEVEL_ERROR = 1;

//TODO read more about constants
//const QString YabetLogHandler::DEFAULT_LOG_LEVEL_FILENAME("log.properties");

YabetLogHandler::YabetLogHandler(QString className)
{
    this->className = className;

    QSettings settings("log.properties", QSettings::IniFormat);

    QString level = settings.value(className, "").toString();
    if(level.isEmpty() == true) {
        level = settings.value("all", "info & error").toString();
    }

    logLevel = 0;

    QStringList levels = level.split(QRegExp::escape("&"));
    foreach(QString currentLvl, levels) {
        if(currentLvl.toLower().trimmed() == "info") {
            logLevel |= LOG_LEVEL_INFO;
        } else if(currentLvl.toLower().trimmed() == "debug") {
            logLevel |= LOG_LEVEL_DEBUG;
        } else if(currentLvl.toLower().trimmed() == "warning") {
            logLevel |= LOG_LEVEL_WARNING;
        } else {
            logLevel |= LOG_LEVEL_ERROR;
        }
    }
}

void YabetLogHandler::debugMsg(QString msg) const {
    if(isEnabled(LOG_LEVEL_DEBUG)) {
        qDebug() << "Debug [" << className <<"]: " << msg;
    }
}

void YabetLogHandler::errorMsg(QString msg) const {
    if(isEnabled(LOG_LEVEL_ERROR)) {
        qCritical() << "Debug [" << className <<"]: " << msg;
    }
}

void YabetLogHandler::infoMsg(QString msg) const {
    if(isEnabled(LOG_LEVEL_INFO)) {
        qDebug() << "Info [" << className <<"]: " << msg;
    }
}

void YabetLogHandler::warningMsg(QString msg) const {
    if(isEnabled(LOG_LEVEL_WARNING)) {
        qWarning() << "Warning [" << className <<"]: " << msg;
    }
}

void YabetLogHandler::msg(QString msg, int logLevel) const {
    if(isEnabled(logLevel)) {
        qDebug() << "Msg [" << className <<"]: " << msg;
    }
}

bool YabetLogHandler::isInfoEnabled() const {
    return isEnabled(LOG_LEVEL_INFO);
}

bool YabetLogHandler::isDebugEnabled() const {
    return isEnabled(LOG_LEVEL_DEBUG);
}

bool YabetLogHandler::isWarningEnabled() const {
    return isEnabled(LOG_LEVEL_WARNING);
}

bool YabetLogHandler::isErrorEnabled() const {
    return isEnabled(LOG_LEVEL_ERROR);
}

bool YabetLogHandler::isEnabled(int level) const {
    int value = logLevel & level;

    return value == level;
}
